package com.itheima.controller;

import com.itheima.common.R;

import com.itheima.constant.CommonConstant;
import com.itheima.dto.InsertUserDTO;
import com.itheima.dto.LoginDTO;
import com.itheima.entity.Employee;
import com.itheima.service.EmployeeService;
import com.itheima.vo.EmployeeVO;
import com.itheima.vo.ResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigInteger;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author msi
 */
@RequestMapping("/employee")
@RestController
public class EmployeeController {
    @Autowired
    private EmployeeService service;

    @Resource
    RedisTemplate<Object, Object> redisTemplate;

    /**
     * 登陆接口
     *
     * @param loginDTO 用户名和密码
     * @return
     */
    @PostMapping("/login")
    public R<EmployeeVO> login(@RequestBody LoginDTO loginDTO) {

        String password = loginDTO.getPassword();
        String username = loginDTO.getUsername();
        Employee employee = service.getLoginResult(username);
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        EmployeeVO employeeVO = new EmployeeVO();

        if (employee == null) {
            return R.error("登陆失败");
        }
        if (!password.equals(employee.getPassword())) {
            return R.error("登陆失败");
        }
        BigInteger status = employee.getStatus();
        String s = status.toString();
        if (CommonConstant.ACCOUNT_BANNED.equals(s)) {
            return R.error("你被封号了");
        }
        //生成touken
        String token = UUID.randomUUID() + "";
        token = token.replace("-", "");
        BeanUtils.copyProperties(employee, employeeVO);
        employeeVO.setToken(token);

        redisTemplate.opsForValue().set(token, employeeVO, 7, TimeUnit.DAYS);


        return R.success(employeeVO);
    }

    /**
     * 登出接口
     *
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        if (Objects.nonNull(token)) {
            redisTemplate.delete(token);
        }

        return R.success("登出成功");
    }


    /**
     * H03-根据id查询员工接口
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getUserById(@PathVariable String id) {

        Long sid = new Long(id);
        Employee user = service.getUser(sid);

        return R.success(user);
    }

    /**
     * 新增
     *
     * @param userDTO
     * @return
     */
    @PostMapping("")
    public R<Object> insertUser(@RequestBody InsertUserDTO userDTO) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(userDTO, employee);
        Boolean flag = service.insertUser(employee);
        if (!flag) {
            return R.error(employee.getName() + "已存在");
        }
        return R.success(null);
    }

    /**
     * 修改
     *
     * @param employee
     * @return
     */
    @PutMapping("")
    public R<Object> updateUser(@RequestBody Employee employee) {
        Boolean flag = service.updateUser(employee);

        return flag ? R.success("修改成功") : R.error("修改失败");
    }

    /**
     * 分页查询
     *
     * @param name
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<ResultVO> listLimit(@RequestParam(value = "name", required = false) String name, @RequestParam("page") Long page,
                                 @RequestParam("pageSize") Long pageSize) {

        ResultVO resultVO = service.listLimit(name, page, pageSize);

        return R.success(resultVO);
    }
}
